package com.qf.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fc.Param.TopicAdd;
import com.qf.fc.dao.TopicDao;
import com.qf.fc.entity.Topic;
import com.qf.fc.service.TopicService;
import com.qf.fc.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 动态话题表(Topic)表服务实现类
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
@Service("topicService")
@RequiredArgsConstructor
public class TopicServiceImpl implements TopicService {
    private  final TopicDao dao;

    @Override
    public R add(TopicAdd add) {
        //1.验证
        if (add==null && StringUtils.hasLength(add.getName())){
            return R.fail("亲，请检查参数信息");
        }
        //验证名称是否存在
        if (dao.selectOne(new LambdaQueryWrapper<Topic>().eq(Topic::getName,add.getName()))!=null){
            return R.fail("亲，该话题已存在。");
        }
        //2.组装对象，完成新增
        Topic topic = new Topic();
        BeanUtils.copyProperties(add,topic);
        topic.setCreateTime(new Date());
        if (dao.insert(topic)>0){
            return R.ok("");
        }
        return R.fail("亲，网络异常，请检查");
    }

    @Override
    public R queryTop() {
        //组装查询条件
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Topic::getCreateTime);
        wrapper.last("limit 6");
        //查询并返回
        return R.ok(dao.selectList(wrapper));
    }

    @Override
    public R queryLike(String name) {
        //组装查询条件
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Topic::getName,name);
        wrapper.orderByDesc(Topic::getCreateTime);
        //查询并返回
        return R.ok(dao.selectList(wrapper));
    }
}

